class Novel < ActiveRecord::Base
  LINK_RULE_1 = 1
  LINK_RULE_2 = 2
  LINK_RULE_3 = 3

  LINK_RULE_1_DESC="只有发起者才能决定续写的片段是否能够被并入文章"
  LINK_RULE_2_DESC="用户可以给续写片段投票，当票数满足条件的时候会通知发起者并由发起者决定是否并入"
  LINK_RULE_3_DESC="用户可以给续写片段投票，当票数满足条件的时候片段会被自动并入文章"

  NOVEL_OVER = 1

  def is_over
    self.over==NOVEL_OVER
  end

  def is_open
    self.over != NOVEL_OVER
  end

  belongs_to :user
  has_many :chapters,:order=>'sequence'
  has_many :comments
  acts_as_tree

  validates_presence_of :summary, :message=>"请提供文章说明"
  validates_presence_of :title, :message=>"请给出文章标题"
  validates_presence_of :user_id, :message=>"你可能未登录，未登录用户不允许创建文章主题"

  validates_length_of :title, :minimum=>1, :message=>"文章标题长度需要在1-70个字之间！"
  validates_length_of :title, :maximum=>70, :message=>"文章标题长度需要在1-70个字之间！"
  validates_length_of :summary, :minimum=>1, :message=>"文章摘要说明长度需要在1-5000个字之间！"
  validates_length_of :summary, :maximum=>5000, :message=>"文章摘要说明长度需要在1-5000个字之间！"
  
    #tag system declare 
  #use an open source tag system plugin: http://wiki.github.com/mbleigh/acts-as-taggable-on/ 
  acts_as_taggable
  #acts_as_taggable_on :skills, :interests
  
  def create_next_chapter
	next_chapter=Chapter.new({:novel_id=>self.id,:sequence=>(self.chapters.last.sequence+1) || 1})
	next_chapter.save
	next_chapter
  end
  
  def current_chapter
	self.chapters.last
  end
end
